#ifndef _DATABASE_H
#define _DATABASE_H

#include "yamlHandler.h"

#define LINESPERMAP_IDNAMEFILE  7
#define LINESPERMAP_SCORCONFIG  4
#define LINESPERMAP_STUDCONFIG  11

typedef struct {
	std::string subject;
	std::string examdate;
	int score;
}subjectScore;

//保存时两个文件都要保存
typedef struct{
    //保存在快速检索表里的基本信息
    //通过searchList::searchUser来读取
    std::string id;
    std::string name;
    std::string username;
    std::string password;
    std::string major;
    std::string grade;
    std::string _class;
    //保存在数据库中的具体信息
    //通过StudentData::read来读取
    std::string sex;
    std::string phone;
    std::string email;
    std::string birthday;
    std::string pid;
    std::string address;
    std::string remark;
    std::vector<subjectScore> scores;
} studentConfig;

//学生信息检索表
class searchList : public YamlHandler
{
public:
    //显式调用父类的构造函数
    searchList() : YamlHandler(LINESPERMAP_IDNAMEFILE){
        this->loadFile("./data/id_name.yaml");
    }
    bool update(studentConfig new_config);
    int searchUser(std::string lable, std::string key, studentConfig& user);
};

//配置每个班级考试科目的接口
class subjectConfig : public YamlHandler
{
    std::vector<subjectScore> subjects;
public:
    subjectConfig() : YamlHandler(LINESPERMAP_SCORCONFIG){}
    void setClass(std::string major, std::string grade, std::string _class);
    void addSubject(std::string subject, int maxscore, int passscore, std::string examedate);
    std::vector<subjectScore> getTotalSubjects();
};

class DataBaseRW : public YamlHandler
{
    bool user_is_changed;
protected:
    bool userisChanged();
    studentConfig student_config;
public:
    DataBaseRW() : YamlHandler(LINESPERMAP_STUDCONFIG){}
    int index();
    bool setUser(studentConfig user);
    //通用函数
    std::string readLable(std::string lable);
    bool writeLable(std::string lable, std::string content);
};

//数据库应用层接口
class StudentData : public DataBaseRW
{
public:
    StudentData(){}
    void refresh(studentConfig config){
        setUser(config);
        read();
    }
    std::string zip();
    void unzip(std::string);
    bool read();
    virtual bool save(std::string config_string);
    studentConfig getstudentConfig();
};

//具有管理员权限的数据库接口
class StudentDataManage : public StudentData
{
public:
    StudentDataManage(){}
    bool save(std::string config_string);
    void addStudent(std::string config_string);
    void removeStudentFromClass();
};

#endif